﻿using System;
using System.Windows.Forms;

namespace test
{
    public class StringTab : MyTab
    {
        private Button actionButton;
        private Button copyButton;
        private TextBox srcTextBox;
        private TextBox targetTextBox;
        private Label srcLabel;
        
        private bool firstClick;
        private String url;

        public override void Init()
        {
            // this.TabPage.Location = new System.Drawing.Point(4, 25);
            this.TabPage.Name = "TabPage_SqlUtil";
            this.TabPage.Size = new System.Drawing.Size(1211, 496);
            this.TabPage.TabIndex = 3;
            this.TabPage.Text = text;
            this.TabPage.UseVisualStyleBackColor = true;

            actionButton = new System.Windows.Forms.Button();
            copyButton = new System.Windows.Forms.Button();
            srcTextBox = new System.Windows.Forms.TextBox();
            targetTextBox = new System.Windows.Forms.TextBox();
            srcLabel = new System.Windows.Forms.Label();

            TabPage.Controls.Add(actionButton);
            TabPage.Controls.Add(copyButton);
            TabPage.Controls.Add(srcTextBox);
            TabPage.Controls.Add(targetTextBox);
            TabPage.Controls.Add(srcLabel);
            // srcLabel = new System.Windows.Forms.Label();

            copyButton.Location = new System.Drawing.Point(511, 466);
            copyButton.Size = new System.Drawing.Size(131, 30);
            copyButton.TabIndex = 5;
            copyButton.Text = "复制到粘贴板";
            copyButton.UseVisualStyleBackColor = true;
            copyButton.Click += new System.EventHandler(this.CopyButtonClick);
            
            actionButton.Location = new System.Drawing.Point(511, 187);
            actionButton.Name = "TopTag_Json_Control_Button";
            actionButton.Size = new System.Drawing.Size(131, 34);
            actionButton.TabIndex = 4;
            actionButton.Text = "格式化";
            actionButton.UseVisualStyleBackColor = true;
            actionButton.Click += new System.EventHandler(ActionButtonClick);
            
            targetTextBox.Location = new System.Drawing.Point(28, 227);
            targetTextBox.Multiline = true;
            targetTextBox.ReadOnly = true;
            targetTextBox.Size = new System.Drawing.Size(1104, 237);
            targetTextBox.TabIndex = 3;
            
            srcTextBox.Location = new System.Drawing.Point(28, 37);
            srcTextBox.Multiline = true;
            srcTextBox.Size = new System.Drawing.Size(1104, 144);
            srcTextBox.TabIndex = 1;
            srcTextBox.Text = "{\"code\": 0,\"message\": \"成功\",\"data\": \"123\"}";
            srcTextBox.Click += new System.EventHandler(srcTextBoxClick);
            
            srcLabel.AutoSize = true;
            srcLabel.Location = new System.Drawing.Point(469, 3);
            srcLabel.Name = "TopTag_Json_Src_Tabel";
            srcLabel.Size = new System.Drawing.Size(219, 15);
            srcLabel.TabIndex = 0;
            srcLabel.Text = "请放入需要格式化的JSON字符串";
        }

        public StringTab(TabControl topTabControl, string text, String url) : base(topTabControl, text)
        {
            this.url = url;
        }

        private void CopyButtonClick(object sender, EventArgs e)
        {
            String target = targetTextBox.Text;
            if (null != target && target.Length > 0)
            {
                Clipboard.SetDataObject(target);
                copyButton.Text = "已复制到粘贴板";
            }
        }
        
        private void ActionButtonClick(object sender, EventArgs e)
        {
            String src = srcTextBox.Text;
            String target = WebUtils.request(src, url);
            targetTextBox.Text = target;
            copyButton.Text = "复制到粘贴板";
        }
        
        private void srcTextBoxClick(object sender, EventArgs e)
        {
            if(firstClick)
            {
                this.srcTextBox.Text = "";
                firstClick = false;
            }
        }
    }
}